package com.wifipositioner.activity;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.wifipositioner.R;
import com.wifipositioner.wifi.WifiProvider;

public class ViewSignalStrenghtActivity extends Activity{
	
	
	private TableLayout table;
	private Button btn_refrButton;
	private Handler messageHandler;
	private HashMap<String, Integer> accessPointMap;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewss);
        table = (TableLayout)findViewById(R.id.view_SS_Table);
        btn_refrButton = (Button) findViewById(R.id.viewss_btn_refresh);
        accessPointMap = new HashMap<String, Integer>();
    }
	
	private void addRow(String col1, String col2, String col3){
        /* Create a new row to be added. */
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
        
             TextView view1 = new TextView(this);
             view1.setText(col1);
             view1.setLayoutParams(new LayoutParams(0));
             view1.setPadding(3, 3, 3, 3);
             
             TextView view2 = new TextView(this);
             view2.setLayoutParams(new LayoutParams(1));
             view2.setText(col2);
             view2.setPadding(3, 3, 3, 3);
             
             TextView view3 = new TextView(this);
             view3.setLayoutParams(new LayoutParams(2));
             view3.setText(col3);
             view3.setPadding(3, 3, 3, 3);
             
             tr.addView(view1);
             tr.addView(view2);
             tr.addView(view3);
  
             table.addView(tr, new TableLayout.LayoutParams(
             LayoutParams.FILL_PARENT,
             LayoutParams.WRAP_CONTENT));

	}
	private void registerHandlers(){
		messageHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {  
            	table.removeViews(1, table.getChildCount() - 1);
                List<ScanResult> results = (List<ScanResult>) msg.obj;
                for(ScanResult res : results){ 	
                	addRow(res.SSID, res.level + "", calcDifference(res));
                	accessPointMap.put(res.SSID, res.level);
                }
            }

        };
        WifiProvider.get().registerHandler(messageHandler);
		
		btn_refrButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				WifiProvider.get().scan();
				
			}
		});
	}
	@Override
	protected void onStart() {
		registerHandlers();
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		WifiProvider.get().removeHandler(messageHandler);
		super.onStop();
	}
	private String calcDifference(ScanResult result){
		Integer level = accessPointMap.get(result.SSID);
		if(level == null)
			return "";
		else
			return Integer.toString(level - result.level);
		
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	menu.add(Menu.NONE,0,0,"Refresh");
    	menu.add(Menu.NONE,1,1,"Positioning");
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
	    switch (item.getItemId()) {
	       case 0: { 
	    	    
	    	   break;
	       }
	       case 1: {
	    	   WifiProvider.get().scan();
    	       break;
	       }		
	    }
		return false;
    }

}
